/**
 * Created by YAAP on 2017/8/3.
 */
define(['jquery'],function ($) {
    var template=
        `<div style="display:inline">
         <button @click="toggle_batch_build">批量生成楼栋</button>
        <Modal
                title="批量生成楼栋【海南大厦】"
                v-model="show"
                width="475"
                class-name="batchCreateBdNum"
        >
            <div class="creatBuildBox">

                <div class="creatBuildBox-top">
                    <div class="treeList">
                        <ul class="clearfix">
                            <li class="list1 pr">
                                <span>楼层名</span>
                                <i>前缀</i>
                                <input type="text" v-model="BuildingRule.NamePrefix"/>
                                <em></em>
                            </li>
                            <li class="list2">
                                <div class="treeNum clearfix">
                                    <Checkbox v-model="useDigitalBuildingNo" ></Checkbox>
                                    <input type="text" v-model="buildingNoBegin"/>
                                    <span>至</span>
                                    <input type="text" v-model="buildingNoEnd"/>
                                </div>
                                <div class="treeNum treeLetter clearfix">
                                    <Checkbox v-model="useCharactorBuildingNo"></Checkbox>
                                    <input type="text"  v-model="buildingNoBeginCharactor"/>
                                    <span>至</span>
                                    <input type="text" v-model="buildingNoEndCharactor"/>
                                </div>
                            </li>
                            <li class="list1 pr">
                                <em class="tree2"></em>
                                <i>后缀</i>
                                <input type="text" v-model="BuildingRule.NameSuffix"/>
                            </li>
                        </ul>
                    </div>
                    <div class="normalList">
                        <ul>
                            <li>
                                <span>总层数</span>
                                <input type="text" v-model="BuildingRule.BuildingAmount"/>
                                <em>层</em>
                            </li>
                            <li>
                                <span>梯户数</span>
                                <input type="text" v-model="BuildingRule.LadderAmountPerUnit"/>
                                <em class="mr14">梯</em>
                                <input type="text" v-model="BuildingRule.RoomAmountPerFloor"/>
                                <em>户</em>
                            </li>
                            <li>
                                <span>单元数</span>
                                <Select v-model="unitModel" style="width:115px">
                                    <Option v-for="item in unitList" :value="item.value"
                                            :key="item">{{ item.label }}
                                    </Option>
                                </Select>
                                <input type="text" class="ml5" v-model="BuildingRule.UnitAmountPerBuilding"/>
                                <em>单元</em>
                            </li>
                        </ul>
                    </div>
                    <div class="creatTable">
                        <div class="table-wrap">
                            <table class="table-wrap-content" border="0">
                                <tr>
                                    <th>楼栋名预览</th>
                                    <th>总层数</th>
                                    <th>梯户数</th>
                                    <th>单元数</th>

                                </tr>
                                <tr v-for="item in previewBuildList">
                                    <td>{{item.BuildingName}}</td>
                                    <td>{{item.FloorAmount}}</td>
                                    <td>{{item.LadderAmount}}T{{item.RoomAmountPerFloor}}H</td>
                                    <td>{{item.UnitAmountPerBuilding}}</td>
                                </tr>

                            </table>
                        </div>


                    </div>
                </div>
            </div>
            <div class="creatBuildBox-btm">
                <button class="preview" v-on:click="preview()"><span>预览</span></button>
                <button class="create" v-on:click="batchCreate()"><span>批量生成楼栋</span></button>
                <button class="cancel"><span>取消</span></button>
            </div>
        </Modal>
        </div>`;
    var BatchBuilding={
        name:"batch-building",
        template:template,
        props:["blockid"],
        computed:{
            useDigitalBuildingNo:function(){return this.BuildingRule.UseDigitalForBuildingNo;},
            useCharactorBuildingNo:function(){return !(this.BuildingRule.UseDigitalForBuildingNo);},
            buildingNoBegin:function(){
                return 1;
            },
            buildingNoEnd:function(){
                return this.BuildingRule.BuildingAmount;
            },
            buildingNoBeginCharactor:function(){
                return String.fromCharCode(64+ this.buildingNoBegin);
            },
            buildingNoEndCharactor:function(){
                return String.fromCharCode(64+ this.buildingNoEnd);
            },

        },
        data:function()
        {
            return {
                BuildingRule:{

                },

                show:false,
                unitList:[],
                previewBuildList:[],
                unitModel:0,


            }
        },
        methods:{
            toggle_batch_build:function(){
                var that=this;
                this.show=!this.show;
                $.ajax(
                    {
                        url: window.config.UrlGetBlockBuildingRule(),
                        error:function(){
                            that.$Messsage.error("Error")
                        },
                        success:function(data)
                        {
                            if(data.IsSuccess)
                            {
                                that.BuildingRule=data.ResultObject;
                            }
                            else
                            {
                                that.$Messsage.error("False")
                            }
                        }

                    }
                );
            },
            preview:function(){
                var that=this;
                console.log(  window.config.UrlGetBatchBuildingPreview());
                $.get(
                      window.config.UrlGetBatchBuildingPreview(),
                  function(previewList)
                        {
                            if(previewList.IsSuccess)
                            {
                                that.previewBuildList=previewList.ResultObject;
                            }
                            else
                            {
                                that.$Message.error("False")
                            }
                        });

            },
            batchCreate:function(){
                var that=this;
                that.preview();
                $.post(window.config.UrlBatchBuildingCreate(),
                    that.previewBuildList,
                    function(result)
                    {
                        if(result.IsSuccess)
                        {
                            that.$Message.success("Success");
                            window.bus.$emit("batchBuidingCreated",that.previewBuildList)
                        }
                        else {
                            that.$Message.error("Fail");
                        }
                    }
                    )
            }
        },

    }
    return BatchBuilding
})
